//
//  AppDelegate.m
//  AppointmentForCar
//
//  Created by Herron on 16/9/26.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "AppDelegate.h"
#import <MMDrawerController.h>
#import "UIViewController+MMDrawerController.h"
#import "HomeViewController.h"
#import "MyCenterViewController.h"
#import "MMDrawerController.h"
#import "LoginViewController.h"

@interface AppDelegate ()<BMKGeneralDelegate>

@property (nonatomic, strong) BMKMapManager *mapManager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"Cwk8EqzgEBFmcU8iIoO24xFmm9bp0r2F" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }

    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self setRootViewController];
    [self setNavigationBarAppearances];
    if([UserAccountManager shareUserManager].loginStatus)
    {
        [self noticeServicerPushClientId];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}
/**
 *  推送相关
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    [[PushManager sharePushManager] setPushInfo:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    [GeTuiSdk resume];
    completionHandler(UIBackgroundFetchResultNewData);
}
/** 远程通知注册成功委托 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    // [ GTSdk ]：向个推服务器注册deviceToken
    [GeTuiSdk registerDeviceToken:token];
    [PushManager sharePushManager].cid = token;
}


#pragma mark - Private Method
- (void)setRootViewController {
    if([UserAccountManager shareUserManager].loginStatus) {
        [self.window setRootViewController:[self homeController]];
    } else {
        [self.window setRootViewController:[self loginController]];
    }
    [self.window makeKeyAndVisible];
}

- (UIViewController *)loginController
{
    LoginViewController * controller = [[LoginViewController alloc] init];
    return controller;
}

- (UIViewController *)homeController {
    UIViewController * myCenterVC = [[MyCenterViewController alloc] init];
    UINavigationController *leftNav = [[UINavigationController alloc] initWithRootViewController:myCenterVC];
    HomeViewController *controller = [[HomeViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    MMDrawerController * drawerController = [[MMDrawerController alloc]
                                             initWithCenterViewController:nav
                                             leftDrawerViewController:leftNav
                                             rightDrawerViewController:nil];
    [drawerController setShowsShadow:YES];
    [drawerController setMaximumLeftDrawerWidth:200];
    [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    [drawerController showsShadow];
    return drawerController;
}

- (void)setNavigationBarAppearances {
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18], NSForegroundColorAttributeName:[UIColor blackColor]}];
    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setTintColor:kColorFontGray];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
}

#pragma mark - <BMKGeneralDelegate>
- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}
#pragma mark - netWork
// 通知服务器记录客户端的推送号
- (void)noticeServicerPushClientId
{
    [[PushManager sharePushManager] getClientID:^(NSString *cid) {
        [NetWork postNetWorkWithUrl:@"push-notice-clientId" parame:@{@"clientId":cid,@"osType":@"ios",@"id":[UserAccountManager shareUserManager].user.userId,@"type":@"1"} successBlock:^(NSDictionary *dic) {
            
        } errorBlock:^(id error) {
            
        }];
    }];
    
}
@end
